package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCarrier;
import cn.stylefeng.guns.modular.basic.entity.BasicLine;
import cn.stylefeng.guns.modular.basic.entity.BasicLoadingQuotation;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicLineParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicLoadingQuotationParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicLineResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicLineService;
import cn.stylefeng.guns.modular.basic.service.BasicLoadingQuotationService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 装车报价表控制器
 *
 * @author zhucx
 * @Date 2024-01-26 11:00:29
 */
@Controller
@RequestMapping("/basicLoadingQuotation")
public class BasicLoadingQuotationController extends BaseController {

    private String PREFIX = "/basic/basicLoadingQuotation";

    @Autowired
    private BasicLoadingQuotationService basicLoadingQuotationService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    @Autowired
    private BasicLineService basicLineService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2024-01-26
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicLoadingQuotation.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2024-01-26
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        //承运商
        BasicCarrierParam carrierParam = new BasicCarrierParam();
        carrierParam.setDeliverId(deliverId);
        carrierParam.setDeptId(deptId);
        carrierParam.setOperateId(user.getOperateId());
        List<BasicCarrierResult> carrierLists = basicCarrierService.findListBySpec(carrierParam, user.getOperateId());
        model.addAttribute("carrierLists", carrierLists);
        //线路
        BasicLineParam lineParam = new BasicLineParam();
        lineParam.setDeliverId(deliverId);
        lineParam.setDeptId(deptId);
        if(user.getOperateId() != null){
            lineParam.setOperateId(user.getOperateId().intValue());
        }
        List<BasicLineResult> lineLists = basicLineService.findListBySpec(lineParam, user.getOperateId());
        model.addAttribute("lineLists", lineLists);
        return PREFIX + "/basicLoadingQuotation_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2024-01-26
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        //承运商
        BasicCarrierParam carrierParam = new BasicCarrierParam();
        carrierParam.setDeliverId(deliverId);
        carrierParam.setDeptId(deptId);
        carrierParam.setOperateId(user.getOperateId());
        List<BasicCarrierResult> carrierLists = basicCarrierService.findListBySpec(carrierParam, user.getOperateId());
        model.addAttribute("carrierLists", carrierLists);
        //线路
        BasicLineParam lineParam = new BasicLineParam();
        lineParam.setDeliverId(deliverId);
        lineParam.setDeptId(deptId);
        if(user.getOperateId() != null){
            lineParam.setOperateId(user.getOperateId().intValue());
        }
        List<BasicLineResult> lineLists = basicLineService.findListBySpec(lineParam, user.getOperateId());
        model.addAttribute("lineLists", lineLists);
        return PREFIX + "/basicLoadingQuotation_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2024-01-26
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicLoadingQuotationParam basicLoadingQuotationParam) {
    	User user = userService.getcurrentUser();
    	Object operateId = basicLoadingQuotationParam.getOperateId();
    	if (operateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(operateId.toString()));
    		basicLoadingQuotationParam.setOperateName(Operate.getOperateName());
		}
    	if(basicLoadingQuotationParam.getCarrierId() != null){
            BasicCarrier carrier = basicCarrierService.getById(basicLoadingQuotationParam.getCarrierId());
            basicLoadingQuotationParam.setCarrierName(carrier.getCarrierName());
        }
        if(basicLoadingQuotationParam.getLineId() != null){
            BasicLine line = basicLineService.getById(basicLoadingQuotationParam.getLineId());
            basicLoadingQuotationParam.setLineName(line.getLineName());
        }
    	basicLoadingQuotationParam.setDeptId(user.getDeptId());
    	basicLoadingQuotationParam.setDeliverId(user.getDeliverId());
    	basicLoadingQuotationParam.setCreateUserName(user.getName());
    	basicLoadingQuotationParam.setState("1");
        this.basicLoadingQuotationService.add(basicLoadingQuotationParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2024-01-26
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicLoadingQuotationParam basicLoadingQuotationParam) {
    	Object operateId = basicLoadingQuotationParam.getOperateId();
    	if (operateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(operateId.toString()));
    		basicLoadingQuotationParam.setOperateName(Operate.getOperateName());
		}
        if(basicLoadingQuotationParam.getCarrierId() != null){
            BasicCarrier carrier = basicCarrierService.getById(basicLoadingQuotationParam.getCarrierId());
            basicLoadingQuotationParam.setCarrierName(carrier.getCarrierName());
        }
        if(basicLoadingQuotationParam.getLineId() != null){
            BasicLine line = basicLineService.getById(basicLoadingQuotationParam.getLineId());
            basicLoadingQuotationParam.setLineName(line.getLineName());
        }
        this.basicLoadingQuotationService.update(basicLoadingQuotationParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2024-01-26
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicLoadingQuotationParam basicLoadingQuotationParam) {
        this.basicLoadingQuotationService.delete(basicLoadingQuotationParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2024-01-26
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicLoadingQuotationParam basicLoadingQuotationParam) {
        BasicLoadingQuotation detail = this.basicLoadingQuotationService.getById(basicLoadingQuotationParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2024-01-26
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicLoadingQuotationParam basicLoadingQuotationParam,String condition ) {
    	User user = userService.getcurrentUser();
    	basicLoadingQuotationParam.setDeptId(user.getDeptId());
        return this.basicLoadingQuotationService.findPageBySpec(basicLoadingQuotationParam,condition,user.getOperateId());
    }

}


